/*
 * CollaborativeWorksCommentDao.java
 *
 * Created on November 10, 2008, 12:39 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.CollaborativeWorksComment;

/**
 *
 * @author JollkY
 */
public class CollaborativeWorksCommentDao implements Crud {
    private int state;
    private CollaborativeWorksComment cwComment;
    private List cwComments;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of CollaborativeWorksCommentDao */
    public CollaborativeWorksCommentDao() {
    }

    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    public List<Object> getList() {
        cwComments = new ArrayList();
        try {
            em.getTransaction().begin();
            cwComments = em.createQuery("SELECT cwComment FROM CollaborativeWorksComment cwComment").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return cwComments;
    }
    
    public List getListByCommentator(long idCommentator) {
        this.cwComments = new ArrayList();
        try {
            em.getTransaction().begin();
            cwComments = em.createQuery("SELECT cwComment FROM CollaborativeWorksComment cwComment " +
                    "WHERE cwComment.commentator.id="+idCommentator).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return cwComments;
    }
    
    public List getListByCw(long id) {
        cwComments = new ArrayList();
        try {
            em.getTransaction().begin();
            cwComments = em.createQuery("SELECT cwComment FROM CollaborativeWorksComment cwComments " +
                    "WHERE cwComments.collaborativeWork.id="+id).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return cwComments;
    }
    
    public List getListByCwComment(long id) {
        cwComments = new ArrayList();
        try {
            em.getTransaction().begin();
            cwComments = em.createQuery("SELECT cwComment FROM CollaborativeWorksComment cwComments " +
                    "WHERE cwComments.replyFor.id="+id).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return cwComments;
    }

    
    public Object read(long id) {
        cwComment = new CollaborativeWorksComment();
        try {
            em.getTransaction().begin();
            cwComment = em.find(CollaborativeWorksComment.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return cwComment;
    }

    
    public int update(Object obj) {
        cwComment = new CollaborativeWorksComment();
        try {
            em.getTransaction().begin();
            CollaborativeWorksComment objCwComment = (CollaborativeWorksComment) obj;
            cwComment = em.find(CollaborativeWorksComment.class, objCwComment.getId());
            cwComment.setCollaborativeWork(objCwComment.getCollaborativeWork());
            cwComment.setComment(objCwComment.getComment());
            cwComment.setCommentator(objCwComment.getCommentator());
            cwComment.setDatePost(objCwComment.getDatePost());
            cwComment.setDatePublish(objCwComment.getDatePublish());
            cwComment.setDateUpdate(objCwComment.getDateUpdate());
            cwComment.setReplyFor(objCwComment.getReplyFor());
            cwComment.setStatus(objCwComment.getStatus());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    
    public int delete(long id) {
        cwComment = new CollaborativeWorksComment();
        try {
            em.getTransaction().begin();
            cwComment = em.find(CollaborativeWorksComment.class, id);
            em.remove(cwComment);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }
    
}
